
class TestMetaclass(type): # 接收一个type?
  def __new__(cls, name, bases, attrs):
    mappings = dict()
    for k,v in attrs.items():
      print('001', k, v)
      mappings[k] = v
    for k in mappings.keys():
      attrs.pop(k)

    attrs['in_metaclass_get_str'] = lambda self,arg: '%s' % arg
    # 反正是一个随便值
    attrs['in_metaclass_c'] = 2333
    print('002', '||', cls, '||', name, '||', bases, '||', attrs) # class || className || baseClass || attrs
    print('003', attrs)
    return type.__new__(cls, name, bases, attrs)

class Test(object, metaclass = TestMetaclass):
  str = 'static value'
  a = 'A member in Test'
  b = 'Anothermenber inTest'
  def __init__(self, defined_in_constructor): # 并不会被执行
    print('007', "i think I can work")
    self.defined_in_constructor = defined_in_constructor

print('000', 'i think i will not be print first!')
t = Test() # 可以看到，不传递参数都能调用，并不是原来版本的 class Test中的__init__
# print('007', t.in_metaclass_get_str)
print('008', t.in_metaclass_c)
# print('009', t.defined_in_constructor) # 报错，并没有这个属性
print('010', Test.in_metaclass_c,)
# print('011', t.a) # Test' object has no attribute 'a'
# print('012', Test.a) # type object 'Test' has no attribute 'a'
# print('013', type(Test)) # <class '__main__.TestMetaclass'>

